<?php


namespace Logic\Fund;
use Core\Exception;
use Helper\StringHelper;

class FundLogic
{
    private $mFundrecord;


    function __construct()
    {
        $this->mFundrecord = new \FundrecordModel();
    }

    /**
     * 分页获取资金明细数据V1
     */
    public function getFundrecordListV1($params = [],$userInfo =[] ){
        $params['page']  = (int)($params['page']) > 0  ? (int)$params['page'] : 1;
        $params['limit'] = (int)($params['limit']) > 0  ? (int)$params['limit'] : 10;
        $params['type']  = in_array((int)$params['type'], [0,1,2,3,4,5,6,7,8,9,-1]) ? (int)$params['type'] : 0;

        $where = [
            ['hidden', '=', 0],
            ['userId', '=', $userInfo['id']],
        ];
        if((int)$params['year'] > 0 && (int)$params['month'] > 0){
            $params['year']  = StringHelper::handleYear($params['year']);
            $params['month'] = StringHelper::handleMonth($params['month']);
            $targetDate     = $params['year'] . "-" . $params['month'] . "-01";   //指定目标月的第一天
            $where[] = ['recordTime', '>=', StringHelper::getCurMonthFirstDay($targetDate)];
            $where[] = ['recordTime', '<=', StringHelper::getCurMonthLastDay($targetDate)];
        }
        switch ($params['type']) {
            case 1:
                // 1 在线充值
                $where[] = ['operateType', 'in', [100]];
                break;
            case 2:
                // -1 提现成功, 2 提现退回
                $where[] = ['operateType', 'in', [110,111]];
                break;
            case 3:
                // 151 收到还款, 251 好友邀请奖励
                $where[] = ['operateType', 'in', [210,211,501]];
                break;
            case 4:
                // 成功，失败
                $where[] = ['operateType', 'in', [200,201,202]];
                break;
            case 5:
                // 售出+与售出管理费
                $where[] = ['operateType', 'in', [300,301]];
                break;
            case 6:
                // 还款
                $where[] = ['operateType', 'in', [303,304,305,306]];
                break;
            case 7:
                //401 商品付款成功
                $where[] = ['operateType', 'in', [401]];
                break;
            case 8:
                //402 收入
                $where[] = ['operateType', 'in', [402,403,404]];
                break;
            //2020/4/7兼容旧版app 暂时将403 404放置8中
//            case 9:
//                // 退款 403 商品订单已退款 404 商品订单已退款至买家
//                $where[] = ['operateType', 'in', [403,404]];
//                break;
            case -1 :
                $where[] = ['operateType', 'not in', [100,110,111,210,211,501,200,201,202,300,301,303,304,305,306,401,402,403,404]];
                break;
            default:
                //Do Nothing
                break;
        }
        $data = $this->mFundrecord->singleGetList($params['page'], $params['limit'], $where, 'id,handleSum,fundMode,recordTime, operateType as type');
        if (empty($data)){
            throw new Exception(10142);
        }
        return $data;
    }

    /**
     * 分页获取资金明细数据V2
     */
    public function getFundrecordListV2($params = [],$userInfo =[] ,$isApp = false){
        $params['page']  = (int)($params['page']) > 0  ? (int)$params['page'] : 1;
        $params['limit'] = (int)($params['limit']) > 0  ? (int)$params['limit'] : 10;
        $params['type']  = in_array((int)$params['type'], [0,1,2,3,4,5,6,7,8,9,10,11,-1]) ? (int)$params['type'] : 0;

        $where = [
            ['hidden', '=', 0],
            ['user_id', '=', $userInfo['id']],
        ];
        if((int)$params['year'] > 0 && (int)$params['month'] > 0){
            $params['year']  = StringHelper::handleYear($params['year']);
            $params['month'] = StringHelper::handleMonth($params['month']);
            $targetDate     = $params['year'] . "-" . $params['month'] . "-01";   //指定目标月的第一天
            $where[] = ['create_time', '>=', strtotime(StringHelper::getCurMonthFirstDay($targetDate))];
            $where[] = ['create_time', '<=', strtotime(StringHelper::getCurMonthLastDay($targetDate))];
        }

        switch ($params['type']) {
            case 1:
                // 1 在线充值
                $where[] = ['operate_type', 'in', ['RECHARGE']];
                break;
            case 2:
                //  提现成功,提现退回
                $where[] = ['operate_type', 'in', ['WITHDRAW','WITHDRAW_BACK']];
                break;
            case 3:
                // 151 收到还款, 251 好友邀请奖励  todo 还差好友邀请奖励 501
                $where[] = ['operate_type', 'in', ['INVEST_REPAYMENT_CAPITAL','INVEST_REPAYMENT_FEE','INVEST_REPAYMENT_PREMIUM','INVEST_REPAYMENT_BU']];
                break;
            case 4:
                // 成功，失败
                $where[] = ['operate_type', 'in', ['INVEST','INVEST_BACK']];
                break;
            case 5:
                // 售出+与售出管理费
                $where[] = ['operate_type', 'in', ['LOANAUDIT','LOANMANAGE']];
                break;
            case 6:
                // 还款
                $where[] = ['operate_type', 'in', ['REPAYMENT_FEE','REPAYMENT_CAPITAL','REPAYMENT_PREMIUM','REPAYMENT_BU','REPAYMENT_FUWU']];
                break;
            case 7:
                //401 商品付款成功
                $where[] = ['operate_type', 'in', ['MALL_SOLD_OUT']];
                break;
            case 8:
                //402 收入
                $where[] = ['operate_type', 'in', ['MALL_ORDER_SUCCESS','MALL_ORDER_REFUND','MALL_ORDER_REFUND_SELL']];
                break;
            //2020/4/7兼容旧版app 暂时将403 404放置8中
            case 9:
                // 退款 403 商品订单已退款 404 商品订单已退款至买家
                $where[] = ['operate_type', 'in', ['MALL_ORDER_REFUND','MALL_ORDER_REFUND_SELL']];
                break;
            case 10:
                // 转入
                $where[] = ['operate_type', 'in', ['TRANSFER_IN']];
                break;
            case 11:
                // 转出
                $where[] = ['operate_type', 'in', ['TRANSFER_OUT']];
                break;


            case -1 :
                $where[] = ['operate_type', 'not in', ['RECHARGE','WITHDRAW','WITHDRAW_BACK','INVEST_REPAYMENT_CAPITAL','INVEST_REPAYMENT_FEE','INVEST','INVEST_BACK','LOANAUDIT','LOANMANAGE','REPAYMENT_FEE','REPAYMENT_CAPITAL','REPAYMENT_PREMIUM','REPAYMENT_BU','REPAYMENT_FUWU','MALL_SOLD_OUT','MALL_ORDER_SUCCESS','MALL_ORDER_REFUND','MALL_ORDER_REFUND_SELL']];
                break;
            default:
                //Do Nothing
                break;
        }

        // 精选流水
        if(in_array($userInfo['role'],['SPECIAL_SELLER','SPECIAL_BUYER'])) {
            $fundrecordModel = new \FundrecordSpecialModel();
        }elseif($userInfo['is_supplier'] == 'PASSED'){
            $fundrecordModel = new \FundrecordSellerModel();
        }elseif($userInfo['is_agency'] == 'PASSED'){
            $fundrecordModel = new \FundrecordAgencyModel();
        }else{
            $fundrecordModel = new \FundrecordOrdinaryModel();
        }

        $data = $fundrecordModel->singleGetList($params['page'], $params['limit'], $where, 'id,handle_money as handleSum,fund_mode as fundMode,create_time as recordTime, operate_type as type,record_type');
        if (empty($data)){
            throw new Exception(10142);
        }

        return $data;
    }


}